package cn.tedu._05article_test.base.interceptors;

import cn.tedu._05article_test.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        log.debug("进入登录检查拦截器：");
////HttpServletRequest  中封装了诸多请求的相关信息
//        // 获取当前会话
//        HttpSession session = request.getSession();
//        UserVO userVO = (UserVO) session.getAttribute("user");
//        // 检查会话中是否存在用户信息
//        if (userVO == null || session.getAttribute("user") == null) {
//            log.debug("用户未登录，重定向到登录页面");
//            // 重定向到登录页面
//            response.sendRedirect("/login.html");
//            return false;
//        }
//
//        log.debug("用户已登录，继续处理请求");
//        return true;
        log.debug("拦截器...");
        HttpSession session = request.getSession();
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO != null){
            return true;
        }
        response.sendRedirect("/login.html");
        return false;
    }
}
